﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	public partial class UserRole: IUserRole
	{

        private const string SQL_SELECT_ROLE = "SELECT a.RoleID,a.RoleName FROM Role a ,UserRole b , Employee c " +
                                               "WHERE a.RoleID = b.RoleID and c.EmployeeID = b.EmployeeID " +
                                                   "AND c.EmployeeID = {0} ";

        private const string SQL_SELECT_PERMISSION = @" SELECT DISTINCT C.MENUID, C.MENUTITLE, C.MENUTITLE_CN, C.MENUURL, C.PARENTMENUID, C.TARGET,C.MENUSEQUENCEID
                                                            FROM USERROLE A, MenuRoleMapping B ,MENUCONFIG C , Employee d
                                                            WHERE d.EmployeeID = {0} AND A.ROLEID=B.ROLEID AND C.MENUID = B.MENUID AND ISENABLED = 1 AND d.EmployeeID = a.EmployeeID
                                                            ORDER BY C.MENUSEQUENCEID";

        private const string SQL_CHECK_PERMISSION = @" SELECT DISTINCT C.MENUID, C.MENUTITLE, C.MENUTITLE_CN, C.MENUURL, C.PARENTMENUID, C.TARGET,C.MENUSEQUENCEID
                                                            FROM USERROLE A, MenuRoleMapping B ,MENUCONFIG C , Employee d
                                                            WHERE d.EmployeeID = {0} AND C.MENUTITLE_CN = '{1}' AND A.ROLEID=B.ROLEID AND C.MENUID = B.MENUID AND ISENABLED = 1 AND d.EmployeeID = a.EmployeeID
                                                            ORDER BY C.MENUSEQUENCEID";

//        private const string SQL_GET_ROLE = @"SELECT a.RoleID FROM UserRole as a inner join Role as b on a.RoleID = b.RoleID inner join Employee as c on a.EmployeeID = c.EmployeeID
//                                                 Where c.EmployeeID = {0}";

        private const string SQL_ISUNIQUEADMIN = @"SELECT     *
                                                                FROM UserRole INNER JOIN
                                                                    Employee ON UserRole.EmployeeID = Employee.EmployeeID
                                                                WHERE    (Employee.Active = 1) AND (UserRole.EmployeeID <> {0}) AND (UserRole.RoleID = 4)";

        private const string SQL_DELETE_USERROLE = @"DELETE FROM USERROLE WHERE EMPLOYEEID = {0}";

        public DataTable GetRoleListByEmployeeId(int employeeId)
        {
            using (ISession session = factory.OpenSession())
            {
                System.Data.DataSet ds = new DataSet();

                //Create a parameter

                string strSql = string.Format(SQL_SELECT_ROLE, employeeId.ToString());

                DataTable dt = DAO.ExecuteDataTable(session, strSql, "Roles");


                //ds.Tables.Add(dt.Copy());

                return dt;
            }

        }

        public DataSet GetPermissionByEmployeeId(int employeeId)
        {
            using (ISession session = factory.OpenSession())
            {
                System.Data.DataSet ds = new DataSet();

                //Create a parameter

                string strSql = string.Format(SQL_SELECT_PERMISSION, employeeId.ToString());

                DataTable dt = DAO.ExecuteDataTable(session, strSql, "Permission");


                ds.Tables.Add(dt.Copy());

                return ds;
            }

        }

        public bool CheckPermission(int employeeId, string permission)
        {
            using (ISession session = factory.OpenSession())
            {
                System.Data.DataSet ds = new DataSet();

                //Create a parameter

                string strSql = string.Format(SQL_CHECK_PERMISSION, employeeId.ToString(), permission);

                DataTable dt = DAO.ExecuteDataTable(session, strSql, "Permission");

                if (dt.Rows.Count >= 1)
                    return true;
                else
                    return false;

            }
        }

        //Joan.lu 2008-09-19
        //Check if the user is Admin
        public bool IsAdmin(long employeeID)
        {
            using (ISession session = factory.OpenSession())
            {
                //System.Data.DataSet ds = new DataSet();

                //Create a parameter

                string strSql = "SELECT a.* FROM dbo.UserRole a INNER JOIN dbo.Role b ON a.RoleID=b.RoleID WHERE b.RoleName='Admin' AND  a.EmployeeID=" + employeeID.ToString();

                DataTable dt = DAO.ExecuteDataTable(session, strSql);

                if (dt.Rows.Count >0)
                    return true;
                else
                    return false;

            }
        
        }
        //Check if the user is RoleAdmin
        public bool IsRoleAdmin(long employeeID)
        {
            using (ISession session = factory.OpenSession())
            {
                //System.Data.DataSet ds = new DataSet();

                //Create a parameter

                string strSql = "SELECT a.* FROM dbo.UserRole a INNER JOIN dbo.Role b ON a.RoleID=b.RoleID WHERE b.RoleName='RoleAdmin' AND  a.EmployeeID=" + employeeID.ToString();

                DataTable dt = DAO.ExecuteDataTable(session, strSql);

                if (dt.Rows.Count > 0)
                    return true;
                else
                    return false;

            }

        }
        ////Edwin.fan 2008-10-8
        ////Get UserRole by CILAccount
        //public DataTable GetUserRoleByEmployeeId(int employeeId)
        //{
        //    using (ISession session = factory.OpenSession())
        //    {
        //        //Create a parameter

        //        string strSql = string.Format(SQL_GET_ROLE, employeeId.ToString());

        //        DataTable dt = DAO.ExecuteDataTable(session, strSql);

        //        return dt;
        //    }
        //}

        /// <summary>
        /// 是否是系统唯一的一个Admin
        /// </summary>
        /// <param name="employeeId"></param>
        public bool IsUniqueAdmin(int employeeId)
        {
            using (ISession session = factory.OpenSession())
            {
                System.Data.DataSet ds = new DataSet();

                //Create a parameter

                string strSql = string.Format(SQL_ISUNIQUEADMIN, employeeId.ToString());

                DataTable dt = DAO.ExecuteDataTable(session, strSql, "Permission");

                if (dt.Rows.Count == 0)
                    return true;
                else
                    return false;

            }
        }

        public bool DeleteUserRoleByEmployeeId(int employeeId)
        {
            using (ISession session = factory.OpenSession())
            {
                System.Data.DataSet ds = new DataSet();

                //Create a parameter

                string strSql = string.Format(SQL_DELETE_USERROLE, employeeId.ToString());

                DAO.ExecuteNonQuery(session, strSql);

                return true;

            }
 
        }
    }
}

